package com.yy.bolg.service.impl;

import com.yy.bolg.dao.ElectricianDao;
import com.yy.bolg.entity.ElectricianBean;
import com.yy.bolg.exception.LackParameterException;
import com.yy.bolg.exception.OtherThingsException;
import com.yy.bolg.service.ElectricianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 电工Service实现类，对接口里的方法进行构造填充
 * @author enmaoFu
 * @date 2024-11-01
 */
@Service("electricianService")
public class ElectricianServiceImpl implements ElectricianService {

    @Autowired
    private ElectricianDao electricianDao;

    @Override
    public void add(ElectricianBean bean) throws Exception {

        if(StringUtils.isEmpty(bean.getPsElectricianName())){
            throw new LackParameterException("姓名不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianPhone())){
            throw new LackParameterException("手机号不能为空");
        }

        if (null != electricianDao.findOneByPhone(bean.getPsElectricianPhone())) {
            throw new OtherThingsException("该手机号已被使用");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianPwd())){
            throw new LackParameterException("密码不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianNvqImg())){
            throw new LackParameterException("电工资格证不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianProvince()) || StringUtils.isEmpty(bean.getPsElectricianCity()) || StringUtils.isEmpty(bean.getPsElectricianRegion())){
            throw new LackParameterException("常驻地区不能为空");
        }

        if(bean.getPsElectricianLongitude() <= 0){
            throw new LackParameterException("经度不合法");
        }

        if(bean.getPsElectricianLatitude() <= 0){
            throw new LackParameterException("纬度不合法");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianAddress())){
            throw new LackParameterException("详细地址不能为空");
        }

        /*if(StringUtils.isEmpty(bean.getPsElectricianHead())){
            throw new LackParameterException("头像不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianIdCode())){
            throw new LackParameterException("身份证号码不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianIdFrontImg())){
            throw new LackParameterException("身份证国徽面不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsElectricianIdBackImg())){
            throw new LackParameterException("身份证人像面不能为空");
        }*/

        try {
            electricianDao.add(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("添加数据失败");
        }
    }

    @Override
    public ElectricianBean findOneByPhone(ElectricianBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsElectricianPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        try {
            return electricianDao.findOneByPhone(bean.getPsElectricianPhone());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public ElectricianBean findOneByCode(ElectricianBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsElectricianCode())) {
            throw new LackParameterException("电工编号不能为空");
        }

        try {
            return electricianDao.findOneByCode(bean.getPsElectricianCode());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public List<ElectricianBean> findAllByCondition(ElectricianBean bean) throws Exception {
        try {
            return electricianDao.findAllByCondition(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public List<ElectricianBean> findAll() throws Exception {
        try {
            return electricianDao.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public void del(ElectricianBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsElectricianPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == electricianDao.findOneByPhone(bean.getPsElectricianPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            electricianDao.del(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("删除数据错误");
        }
    }

    @Override
    public void up(ElectricianBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsElectricianPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == electricianDao.findOneByPhone(bean.getPsElectricianPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            electricianDao.update(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

    @Override
    public String signIn(ElectricianBean bean) throws Exception {
        String code = "";

        if (StringUtils.isEmpty(bean.getPsElectricianPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (StringUtils.isEmpty(bean.getPsElectricianPwd())) {
            throw new LackParameterException("密码不能为空");
        }

        ElectricianBean electricianBean = electricianDao.findOneByPhone(bean.getPsElectricianPhone());

        if (null == electricianBean) {
            throw new OtherThingsException("手机号或密码错误");
        }else{
            if(!bean.getPsElectricianPhone().equals(electricianBean.getPsElectricianPhone()) || !bean.getPsElectricianPwd().equals(electricianBean.getPsElectricianPwd())){
                throw new OtherThingsException("手机号或密码错误");
            }else{
                try {
                    code = electricianBean.getPsElectricianCode();
                    electricianDao.signIn(bean);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new OtherThingsException("登录失败");
                }
            }
        }
        return code;
    }

    @Override
    public void signOut(ElectricianBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsElectricianPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == electricianDao.findOneByPhone(bean.getPsElectricianPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            electricianDao.signOut(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("退出失败");
        }
    }

    @Override
    public void upState(ElectricianBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsElectricianPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == electricianDao.findOneByPhone(bean.getPsElectricianPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            electricianDao.upState(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }
}
